#include "loginwindow.h"
#include "ui_loginwindow.h"
#include <QLineEdit>
#include <QLabel>

loginWindow::loginWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::loginWindow)
{
    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint);
    //QPushButton *exit = QObject::findChild<QPushButton*>("exit_button");
}

loginWindow::~loginWindow()
{
    delete ui;
}

void loginWindow::on_button_login_clicked()
{
    QLineEdit *user = QObject::findChild<QLineEdit*>("text_user");
    QLineEdit *password = QObject::findChild<QLineEdit*>("text_password");
    QLabel *message = QObject::findChild<QLabel*>("label_message");

    std::string username = user->text().toStdString();
    std::string passwd = password->text().toStdString();

    message->setText("登录中...");
    //登录
    client->thread_login(username,passwd);
}

void loginWindow::loginAcc(int ret){
    QLabel *message = QObject::findChild<QLabel*>("label_message");
    if(ret){
        switch(ret){
        case CS_LINKERROR:
            message->setText("登录失败，连接失败！");
            break;
        case CS_LOGIN_SERVERERROR:
        case CS_SENDERROR:
            message->setText("登录失败，服务器通信失败！");
            break;
        case CS_LOGIN_USERERROR:
            message->setText("用户名/密码错误");
            break;
        case CS_REJECTED_LOGIN:
            message->setText("登录失败，拒绝登录。");
            break;
        default:
            message->setText("登录失败，未知错误");
            break;
        }
    }
    else{
        message->setText("请稍等");
        //TODO:用户窗口
    }
}

void loginWindow::on_exit_button_clicked()
{
    QApplication::exit();
}
